package com.fun.easyplugin.service.player;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.fun.easyplugin.spring.properties.MoneyProperties;
import lombok.extern.log4j.Log4j2;
import org.bukkit.entity.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * TODO ${注释}
 *
 * @author <a href="mailto:huangzf1778@fingard.com">huang zifan</a>
 * @version 1.0
 * <p>
 * Copyright (c) 2009 fingard System Engineering Co., Ltd.
 * All rights reserved.
 * <p>
 * This software is the confidential and proprietary information of
 * fingard System Engineering Co., Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered
 * into with fingard.
 * @projectName: easy-plugin
 * @date 2024/6/7
 */
@Service
@Log4j2
public class PlayerService {

    @Autowired
    private MoneyProperties moneyProperties;

    public boolean giveMoney(Player player, BigDecimal money) {
        // 获取玩家当前的金币
        BigDecimal amount = this.queryPlayMoney(player);
        log.debug("玩家{}当前金币{}", player.getName(), amount + moneyProperties.getUnit());
        // 给予玩家金币
        amount = amount.add(money);
        // 保留两位
        amount = NumberUtil.round(amount, 2);
        if (amount.compareTo(BigDecimal.ZERO) < 0 && !moneyProperties.isAllowNegative()) {
            log.warn("玩家{}余额不足", player.getName());
            return false;
        }
        log.info("给予玩家{} {}，玩家现在有{}", player.getName(), money + moneyProperties.getUnit(), amount + moneyProperties.getUnit());
        return true;
    }

    public BigDecimal queryPlayMoney(Player player) {
        // todo 查询玩家的金币
        double curMoney = RandomUtil.randomDouble(100, 1000, 2, RoundingMode.HALF_UP);
        return NumberUtil.toBigDecimal(curMoney);
    }
}
